package com.QSMoney.controller;

import com.QSMoney.Utils.JwtUtil;
import com.QSMoney.Utils.Result;
import com.QSMoney.constants.ResultCode;
import com.QSMoney.entity.VO.LoginMoneyUserVo;
import com.QSMoney.entity.req.LoginMoneyUserReq;
import com.QSMoney.entity.req.MoneyUserReq;
import com.QSMoney.service.ILoginService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.annotation.Resource;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.Date;

@Slf4j
@RestController
@RequestMapping("/user")
@Validated
@Tag(name = "用户接口", description = "用户登录相关接口")
public class MoneyUserController {

    @Resource
    private ILoginService loginService;

    @Autowired
    private JwtUtil jwtUtil;

    @PostMapping("/login")
    @Operation(summary = "用户登录", description = "用户登录接口")
    public Result<LoginMoneyUserVo> login(@RequestBody LoginMoneyUserReq loginMoneyUserReq) {

        LoginMoneyUserVo result=  loginService.login(loginMoneyUserReq);
        String token = jwtUtil.generateToken(loginMoneyUserReq.getUsername());
        result.setToken(token);
       result.setExpires(jwtUtil.getExpirationDateFromToken(token));
        return Result.success("登录成功",result);
    }
    /**
     * 注册
     * @return
     */
    @PostMapping("/register")
    @Operation(summary = "用户注册", description = "用户注册接口")
    public Result<String> register(@RequestBody @Validated MoneyUserReq moneyUserReq) {
        log.info("开始注册");
        return Result.success("注册成功", loginService.register(moneyUserReq));
    }
}
